$(function () {

    var attendance_datagrid = $("#attendance_datagrid");
    var attendance_dialog = $("#attendance_dialog");


    var beginDate = $("#beginDate").datebox('calendar');
    beginDate.calendar({
        validator: function (date) {
            // 判断date和当前时间new Date()的先后
            if (date < new Date()) {
                return true;
            }
        }
    });

    var endDate = $("#endDate").datebox('calendar');
    endDate.calendar({
        validator: function (date) {
            // 判断date和当前时间new Date()的先后
            if (date <= new Date()) {
                return true;
            }
        },
        onSelect:function (date) {
            var oldDate = $("#beginDate").datebox('getValue');
            var newDate = $("#endDate").datebox('getValue');
            $.post("/attendance/judge.do",{newDate:newDate,oldDate:oldDate},function (data) {
                endDate.calendar({
                    validator:function () {
                        if(data.success){
                            return true;
                        } else {
                            return false;
                        }
                    }
                })
            })
        }
    });

    attendance_datagrid.datagrid({
        url: '/attendance/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'id', title: '考勤编号 ', width: 80},
            {
                field: 'employee', title: '员工姓名', width: 80, formatter: function (value) {
                    return value.username
                }
            },
            {field: 'signInDate', title: '签到日期', width: 80},
            {field: 'signInTime', title: '签到时间', width: 80},
            {field: 'signOutTime', title: '签退时间', width: 80},
            {
                field: 'signInState', title: '签到状态', width: 80, formatter: function (value) {
                    if (value != null) {
                        return value ? '签到正常' : '<span style="color: red">迟到</span>'
                    }
                }
            },
            {
                field: 'signOutState', title: '签退状态', width: 80, formatter: function (value) {
                    if (value != null) {
                        return value ? '签退正常' : '<span style="color: red">早退</span>'
                    }
                }
            },
            {field: 'patchTime', title: '补签时间', width: 80}
        ]],
        toolbar: '#attendance_toolbar',
        singleSelect: true,
        rownumbers: true,
        pagination: true
    });
    attendance_dialog.dialog({
        title: '选择日期',
        width: 300,
        height: 350,
        closed: true,
        resizable: false
    });

    //使用一个对象来统一管理实践的方法
    var methodObj = {
        //签到
        signIn: function () {
            $.messager.confirm('确认对话框', '确认签到吗?', function (r) {
                if (r) {
                    $.get("/attendance/signIn.do", function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '签到成功', 'info', function () {
                                attendance_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    })
                }
            })
        },
        //签退
        signOut: function () {
            var row = attendance_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据!', 'error');
                return;
            }
            $.messager.confirm('确认对话框', '确认签退吗?', function (r) {
                if (r) {
                    $.get("/attendance/signOut.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '签退成功', 'info', function () {
                                attendance_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    })
                }
            })
        },
        //刷新
        reload: function () {
            attendance_datagrid.datagrid('reload')
        },
        //查询
        query: function () {
            //获取查询条件的值
            var keywrod = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            attendance_datagrid.datagrid('load', {
                keyword: keywrod,
                beginDate: beginDate,
                endDate: endDate
            });

        },


        //补签
        supplement: function () {
            var row = attendance_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据!', 'error');
                return;
            }
            /*if(row.pathname){
                $.messager.alert('温馨提示','已补签,无需再补!','error');
                return;
            }*/

            attendance_dialog.dialog('open');
            $("#attendance_date").calendar({
                fit: true,
                onSelect: function (date) {

                    $.get("/attendance/supplement.do", {signInDate: date, id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("选择日期", "补签成功", "info", function () {
                                attendance_dialog.dialog("close");
                                attendance_datagrid.datagrid("load");
                            })
                        } else {
                            $.messager.alert("选择日期", data.msg, "info", function () {

                                attendance_dialog.dialog("close");
                                attendance_datagrid.datagrid("load");
                            });
                        }
                    })
                },
                validator: function (date) {
                    // 判断date和当前时间new Date()的先后
                    if (date < new Date()) {
                        return true;
                    } else {
                        //如果date晚于当前时间,return false;
                        return false;
                    }
                }

            });
        }

    };

    //给 a链接统一绑定事件
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        //调用方法
        methodObj[methodName]();
    });
});

